home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Swar / main.c < prev    next >
C/C++ Source or Header  |  1994-08-21  |  4KB  |  206 lines

  1.  
  2. #include "swar.h"
  3. #include "globals.h"
  4.  
  5. main()
  6. {
  7.     short    tics;
  8.     
  9.     MaxApplZone();
  10.     ToolBoxInit();
  11.     GetDateTime((unsigned long *)&randSeed);
  12.     DrawMenuBar();
  13.     WindowInit();
  14.     gDone = FALSE;
  15.     gPlaying = FALSE;
  16.     gPauseOn = FALSE;
  17.     gSoundOn = TRUE;
  18.     gSndIsInitted = FALSE;
  19.     SetChecks();
  20.     LoadPieces();
  21.     LoadSounds();
  22.     InitColors();
  23.     wneAvail = FALSE;
  24.     SysEnvirons(1, &theSysEnv);
  25.     if (theSysEnv.machineType >= 1) {
  26.         if (NGetTrapAddress(0xA860, 1) != 0xA89F)
  27.             wneAvail = TRUE;
  28.     } /* if */
  29.     while (!gDone) {
  30.         if (gPlaying && !gPauseOn) {
  31.             GameCycle();
  32.         } /* if */
  33.         HandleEvent();
  34.     } /* while */
  35.     ExitAppl();
  36.             
  37. } /* main () */
  38.  
  39. HandleEvent()
  40. {
  41.     char    theChar;
  42.     
  43.     if (!wneAvail) {
  44.         GetNextEvent(everyEvent, &gTheEvent);
  45.         SystemTask();
  46.     } /* if */
  47.     else
  48.         WaitNextEvent(everyEvent, &gTheEvent, 0L, 0L);
  49.     
  50.     switch (gTheEvent.what) {
  51.         case mouseDown:
  52.             if ((gPauseOn && gPlaying) || !gPlaying)
  53.                 HandleMouseDown();
  54.             break;
  55.         case keyDown:
  56.         case autoKey:
  57.             theChar = gTheEvent.message & charCodeMask;
  58.             if ((gTheEvent.modifiers & cmdKey) != 0)
  59.                 HandleMenuChoice(MenuKey(theChar));
  60.             break;
  61.         case updateEvt:
  62.         case nullEvent:
  63.         case mouseUp:
  64.         case keyUp:
  65.         case diskEvt:
  66.         case activateEvt:
  67.         case networkEvt:
  68.         case driverEvt:
  69.         case app1Evt:
  70.         case app2Evt:
  71.         case app3Evt:
  72.             break;
  73.         case app4Evt:
  74.             break;
  75.         default:
  76.             break;
  77.     } /* switch */
  78.     
  79. } /* HandleEvent() */
  80.  
  81. HandleMouseDown()
  82. {
  83.     WindowPtr        whichWindow;
  84.     short int        thePart;
  85.     long int            menuChoice, windSize;
  86.     
  87.     thePart = FindWindow(gTheEvent.where, &whichWindow);
  88.     switch (thePart) {
  89.         case inMenuBar:
  90.             menuChoice = MenuSelect(gTheEvent.where);
  91.             HandleMenuChoice(menuChoice);
  92.             break;
  93.         case inSysWindow:
  94.             SystemClick(&gTheEvent, whichWindow);
  95.             break;
  96.         case inDrag:
  97. //            DragWindow(whichWindow, gTheEvent.where, &gDragRect);
  98.             break;
  99.         case inGoAway:
  100. //            gDone = TRUE;
  101.             break;
  102.         case inContent:
  103.         case inGrow:
  104.         case inZoomIn:
  105.         case inZoomOut:
  106.         default:
  107.             break;
  108.     } /* switch */
  109.     
  110. } /* HandleMouseDown() */
  111.  
  112. HandleMenuChoice(menuChoice)
  113.     long int        menuChoice;
  114. {
  115.     int    theMenu;
  116.     int    theItem;
  117.     
  118.     if (menuChoice != 0) {
  119.         theMenu = HiWord(menuChoice);
  120.         theItem = LoWord(menuChoice);
  121.         switch (theMenu) {
  122.             case 500:
  123.                 HandleAppleChoice(theItem);
  124.                 break;
  125.             case 501:
  126.                 HandleFileChoice(theItem);
  127.                 break;
  128.         } /* switch */
  129.         HiliteMenu(0);        
  130.     } /* if */
  131.     
  132. } /* HandleMenuChoice() */
  133.  
  134.  
  135. HandleAppleChoice(theItem)
  136.     int        theItem;
  137. {
  138.     Str255        accName;
  139.     int            accNumber;
  140.     GrafPtr        savePort;
  141.     
  142.     switch (theItem) {
  143.         case 1:
  144.             break;
  145.         default:    
  146.             GetPort(&savePort);
  147.             GetItem(gAppleMenu, theItem, accName);
  148.             accNumber = OpenDeskAcc(accName);
  149.             SetPort(savePort);
  150.             break;
  151.     } /* switch */
  152.     
  153. } /* HandleAppleChoice() */
  154.  
  155. HandleFileChoice(theItem)
  156.     int        theItem;
  157. {
  158.     GrafPtr        savePort;
  159.     
  160.     switch (theItem) {
  161.         case 1:
  162.             EnableItem (gFileMenu, 2);
  163.             EnableItem (gFileMenu, 5);
  164.             gPlaying = TRUE;
  165.             gPauseOn = FALSE;
  166.             InitGame();
  167.             HideCursor();
  168.             GetPort(&savePort);
  169.             SetPort((GrafPtr)gPictureWindow);
  170.             ShowWindow((WindowPtr)gPictureWindow);
  171.             //
  172.             //        Must set FG and BG pens for CopyBits to work
  173.             //    
  174.             RGBForeColor(&myBlack);
  175.             RGBBackColor(&myWhite);
  176.             break;
  177.         case 2:
  178.             if (gPauseOn) {
  179.                 gPauseOn = FALSE;
  180.                 HideCursor();
  181.             } /* if */
  182.             else {
  183.                 gPauseOn = TRUE;
  184.                 ShowCursor();
  185.             } /* else */
  186.             SetChecks();
  187.             break;
  188.         case 3:
  189.             if (gSoundOn)
  190.                 gSoundOn = FALSE;
  191.             else
  192.                 gSoundOn = TRUE;
  193.             SetChecks();
  194.             break;
  195.         case 5:
  196.             break;
  197.         case 7:
  198.             gDone = TRUE;
  199.             EnableItem (gFileMenu, theItem);
  200.             DisableItem (gFileMenu, 2);
  201.             DisableItem (gFileMenu, 5);
  202.             break;
  203.     } /* switch */
  204.     
  205. } /* HandleFileChoice() */
  206.